/*
 * @Description:
 * @Version: 2.0
 * @Author: Xing
 * @Date: 2021-02-06 15:46:22
 * @LastEditors: Xing
 * @LastEditTime: 2021-02-06 16:05:11
 */
import { ref } from 'vue';
import axios from 'axios';
// <T>泛型
function useURLLoader<T>(url: string) {
  const result = ref<T | null>(null);
  const loading = ref(true);
  const loaded = ref(false);
  const error = ref(null);

  axios
    .get(url)
    .then((rawData) => {
      loading.value = false;
      loaded.value = true;
      result.value = rawData.data;
    })
    .catch((e) => {
      error.value = e;
      loading.value = false;
    });

  return {
    result,
    loading,
    error,
    loaded,
  };
}

export default useURLLoader;
